﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;

namespace APP.HELPER.Utils.Security
{
    public class DecryptTransformer
    {
        // Fields
        private EncryptionAlgorithm algorithmID;
        private bool bHasIV;
        private byte[] IV;
        private string SecurityKey;

        // Methods
        public DecryptTransformer(EncryptionAlgorithm algID)
        {
            this.SecurityKey = "";
            this.bHasIV = false;
            this.algorithmID = algID;
        }

        public DecryptTransformer(EncryptionAlgorithm algID, byte[] iv)
        {
            this.SecurityKey = "";
            this.bHasIV = false;
            this.algorithmID = algID;
            this.IV = iv;
            this.bHasIV = true;
        }

        public ICryptoTransform GetCryptoTransform()
        {
            bool flag = false;
            if (this.SecurityKey.Length != 0)
            {
                flag = true;
            }
            byte[] bytes = Encoding.ASCII.GetBytes(this.SecurityKey);
            switch (this.algorithmID)
            {
                case EncryptionAlgorithm.DES:
                    {
                        DES des = new DESCryptoServiceProvider();
                        if (flag)
                        {
                            des.Key = bytes;
                        }
                        if (this.bHasIV)
                        {
                            des.IV = this.IV;
                        }
                        return des.CreateDecryptor();
                    }
                case EncryptionAlgorithm.Rc2:
                    {
                        RC2 rc = new RC2CryptoServiceProvider();
                        if (flag)
                        {
                            rc.Key = bytes;
                        }
                        if (this.bHasIV)
                        {
                            rc.IV = this.IV;
                        }
                        return rc.CreateDecryptor();
                    }
                case EncryptionAlgorithm.Rijndael:
                    {
                        Rijndael rijndael = new RijndaelManaged();
                        if (flag)
                        {
                            rijndael.Key = bytes;
                        }
                        if (this.bHasIV)
                        {
                            rijndael.IV = this.IV;
                        }
                        return rijndael.CreateDecryptor();
                    }
                case EncryptionAlgorithm.TripleDes:
                    {
                        TripleDES edes = new TripleDESCryptoServiceProvider();
                        if (flag)
                        {
                            edes.Key = bytes;
                        }
                        if (this.bHasIV)
                        {
                            edes.IV = this.IV;
                        }
                        return edes.CreateDecryptor();
                    }
            }
            throw new CryptographicException("Algorithm ID '" + this.algorithmID + "' not supported.");
        }

        public void SetSecurityKey(string Key)
        {
            this.SecurityKey = Key;
        }

        // Properties
        public EncryptionAlgorithm EncryptionAlgorithm
        {
            get
            {
                return this.algorithmID;
            }
            set
            {
                this.algorithmID = value;
            }
        }
    }

}
